package com.bytetool.agent.method;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;
import javassist.NotFoundException;

import java.util.Arrays;

/**
 * @author zouhuixing
 * @date 2024/3/31
 */
public class CtClassMethodUtil {

    private static String[] COMMON_METHOD_NAMES = new String[]{"main", "toString", "equals", "hashCode", "finalize"};

    public static CtClass getCtClass(ClassLoader loader, String className) throws NotFoundException {
        ClassPool pool = ClassPool.getDefault();
        pool.appendClassPath(new LoaderClassPath(loader));
        return pool.getCtClass(className.replace("/", "."));
    }

    public static boolean shouldSkipCommonMethod(String methodName) {
        if (Arrays.asList(COMMON_METHOD_NAMES).contains(methodName)) {
            return true;
        }
        return false;
    }

    public static boolean includes(String[] includes, String className) {
        for (String include : includes) {
            if (className.contains(include)) {
                return true;
            }
        }
        return false;
    }
}
